package com.sl.au.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.SaleArea;
import com.sl.au.entity.Section;
import com.sl.au.entity.SectionArea;
import com.sl.au.repository.SectionAreaRepository;
import com.sl.au.repository.SectionRepository;
import com.sl.au.service.SectionService;

@Service("SectionService")
@Transactional
public class SectionServiceImpl extends BaseServiceImpl implements SectionService {

	@Autowired
	SectionRepository sectionRepository;
	
	@Autowired
	SectionAreaRepository sectionAreaRepository;

	@Override
	public SectionRepository getRepository() {
		// TODO Auto-generated method stub
		return this.sectionRepository;
	}
	
	@Override
	public SectionAreaRepository getSARepository() {
		// TODO Auto-generated method stub
		return this.sectionAreaRepository;
	}


	@Override
	public List<Section> getPage(int start, int length, String key) {
		if(key==null){
			key="";
		}else{
			key = key.replaceAll("\\s+", "");
		}
		Page<Section> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = sectionRepository.findSection(key, pageable);
		return pages.getContent();
	}


	@Override
	public List<SectionArea> findSectionArea(int start, int length, String sectionId) {
		Page<SectionArea> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages=sectionRepository.findSectionArea(sectionId, pageable);
		return pages.getContent();
	}


	@Override
	public List<SaleArea> findNoSArea(int start, int length, String sectionId,String superior) {
		Page<SaleArea> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages=sectionRepository.findSaleArea(sectionId,superior, pageable);
		return pages.getContent();
	}

	@Override
	public void patch(Section section) {
		// TODO Auto-generated method stub
		Section s = this.getRepository().findOne(section.getId());
		if (null == s)
			return;
		BeanUtils.copyProperties(section, s,"createTime");
		this.getRepository().save(s);
	}

	@Override
	public List<SectionArea> findSectionAreaOne(String superior, String areaName) {
		List<SectionArea> sectionArea = sectionRepository.findSectionAreaOne(superior, areaName);
		return sectionArea;
	}
	
	


	
	
}
